package A1_3_School;

import java.util.ArrayList;
import java.util.List;

// 课程类
class Subject {
    private String subjectId;
    private String subjectName;
    private int credits;

    public Subject(String subjectId, String subjectName, int credits) {
        this.subjectId = subjectId;
        this.subjectName = subjectName;
        this.credits = credits;
    }

    // Getters and Setters
    public String getSubjectId() { return subjectId; }
    public String getSubjectName() { return subjectName; }
    public int getCredits() { return credits; }

    @Override
    public String toString() {
        return "School.Subject{" + "subjectId='" + subjectId + '\'' +
                ", subjectName='" + subjectName + '\'' +
                ", credits=" + credits + '}';
    }
}

// 学生类
class Student {
    private String studentId;
    private String name;
    private List<Subject> subjects; // 学生与课程的关联关系

    public Student(String studentId, String name) {
        this.studentId = studentId;
        this.name = name;
        this.subjects = new ArrayList<>();
    }

    // 学生参加课程 (1-5门)
    public boolean enrollSubject(Subject subject) {
        if (subjects.size() >= 5) {
            System.out.println("学生最多只能参加5门课程");
            return false;
        }
        if (!subjects.contains(subject)) {
            subjects.add(subject);
            return true;
        }
        return false;
    }

    // Getters and Setters
    public String getStudentId() { return studentId; }
    public String getName() { return name; }
    public List<Subject> getSubjects() { return subjects; }

    @Override
    public String toString() {
        return "School.Student{" + "studentId='" + studentId + '\'' +
                ", name='" + name + '\'' +
                ", subjectsCount=" + subjects.size() + '}';
    }
}

// 指导教师类
class Instructor {
    private String instructorId;
    private String name;
    private List<Subject> teachingSubjects; // 教师与课程的关联关系 (1-3门)

    public Instructor(String instructorId, String name) {
        this.instructorId = instructorId;
        this.name = name;
        this.teachingSubjects = new ArrayList<>();
    }

    // 教师教授课程 (1-3门)
    public boolean assignSubject(Subject subject) {
        if (teachingSubjects.size() >= 3) {
            System.out.println("教师最多只能教授3门课程");
            return false;
        }
        if (!teachingSubjects.contains(subject)) {
            teachingSubjects.add(subject);
            return true;
        }
        return false;
    }

    // Getters and Setters
    public String getInstructorId() { return instructorId; }
    public String getName() { return name; }
    public List<Subject> getTeachingSubjects() { return teachingSubjects; }

    @Override
    public String toString() {
        return "School.Instructor{" + "instructorId='" + instructorId + '\'' +
                ", name='" + name + '\'' +
                ", teachingSubjectsCount=" + teachingSubjects.size() + '}';
    }
}

// 系类
class Department {
    private String departmentId;
    private String departmentName;
    private List<Instructor> instructors; // 系与教师的关联关系
    private List<Subject> subjects; // 系与课程的组合关系

    public Department(String departmentId, String departmentName) {
        this.departmentId = departmentId;
        this.departmentName = departmentName;
        this.instructors = new ArrayList<>();
        this.subjects = new ArrayList<>();
    }

    // 添加教师 (至少1位)
    public void addInstructor(Instructor instructor) {
        if (!instructors.contains(instructor)) {
            instructors.add(instructor);
        }
    }

    // 提供课程 (至少1门)
    public void addSubject(Subject subject) {
        if (!subjects.contains(subject)) {
            subjects.add(subject);
        }
    }

    // Getters and Setters
    public String getDepartmentId() { return departmentId; }
    public String getDepartmentName() { return departmentName; }
    public List<Instructor> getInstructors() { return instructors; }
    public List<Subject> getSubjects() { return subjects; }

    @Override
    public String toString() {
        return "School.Department{" + "departmentId='" + departmentId + '\'' +
                ", departmentName='" + departmentName + '\'' +
                ", instructorsCount=" + instructors.size() +
                ", subjectsCount=" + subjects.size() + '}';
    }
}

// 学院类
class School {
    private String schoolId;
    private String schoolName;
    private List<Department> departments; // 学院与系的组合关系
    private List<Student> students; // 学院与学生的聚合关系

    public School(String schoolId, String schoolName) {
        this.schoolId = schoolId;
        this.schoolName = schoolName;
        this.departments = new ArrayList<>();
        this.students = new ArrayList<>();
    }

    // 添加系
    public void addDepartment(Department department) {
        if (!departments.contains(department)) {
            departments.add(department);
        }
    }

    // 招收学生
    public void enrollStudent(Student student) {
        if (!students.contains(student)) {
            students.add(student);
        }
    }

    // Getters and Setters
    public String getSchoolId() { return schoolId; }
    public String getSchoolName() { return schoolName; }
    public List<Department> getDepartments() { return departments; }
    public List<Student> getStudents() { return students; }

    // 显示学院信息
    public void displaySchoolInfo() {
        System.out.println("=== 学院信息 ===");
        System.out.println("学院ID: " + schoolId);
        System.out.println("学院名称: " + schoolName);
        System.out.println("系数量: " + departments.size());
        System.out.println("学生数量: " + students.size());

        for (Department dept : departments) {
            System.out.println("\n--- " + dept.getDepartmentName() + " ---");
            System.out.println("教师数量: " + dept.getInstructors().size());
            System.out.println("课程数量: " + dept.getSubjects().size());
        }
    }

    @Override
    public String toString() {
        return "School.School{" + "schoolId='" + schoolId + '\'' +
                ", schoolName='" + schoolName + '\'' +
                ", departmentsCount=" + departments.size() +
                ", studentsCount=" + students.size() + '}';
    }
}

